﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraGrid.Views.Grid;
using NNS.Framework;
using NNS.UI;
using QLTB.BusinessLayer;
using QLTB.BusinessObjects;
using QLTB.Common;
using QLTB.LichSu.Entity;

namespace QLTB.LichSu.UI
{
    [System.ComponentModel.ToolboxItem(false)]
    public partial class ucLichSu : MyUIBase
    {
        #region Column enum

        public enum Column
        {
            None,
            MaTB,
            Lock
        }

        #endregion

        private TBL_THIETBI _thietbi;
        private Column m_Column;

        public ucLichSu()
        {
            InitializeComponent();
            TBL_quyenBL.IgnoreError = true;
            if (!TBL_quyenBL.isTaomoi(EnumComponent.iEquipHistory))
            {
                gbList.OptionsView.NewItemRowPosition = NewItemRowPosition.None;
            }
            TBL_quyenBL.IgnoreError = false;
        }

        public TBL_THIETBI ThietBi
        {
            set
            {
                _thietbi = value;
                Nap(_thietbi.ID);
            }
        }

        private void frmLichSu_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'dsLichSu.TBL_lichsu' table. You can move, or remove it, as needed.
            Nap();
        }

        public void Nap()
        {
            if (_thietbi == null) return;
            Nap(_thietbi.ID);
        }

        public void Nap(int id)
        {
            try
            {
                tBL_lichsuTableAdapter.Connection.ConnectionString = DbHelper.ConnectionString;
                tBL_lichsuTableAdapter.FillBy(dsLichSu.TBL_lichsu, id);
            }
            catch (Exception ex)
            {
                MyBox.ShowWarning(ex.Message, "Quản lý thiết bị");
            }
        }

        public override void Save()
        {
            try
            {
                tBL_lichsuTableAdapter.Connection.ConnectionString = DbHelper.ConnectionString;
                tBL_lichsuTableAdapter.Adapter.RowUpdating += Adapter_RowUpdating;
                tBL_lichsuTableAdapter.Adapter.RowUpdated += Adapter_RowUpdated;
                tBL_lichsuTableAdapter.Update(dsLichSu.TBL_lichsu);
                NotSave = false;
            }
            catch (Exception ex)
            {
                MyBox.ShowWarning(ex.Message, "Quản lý thiết bị");
            }
        }

        private void Adapter_RowUpdated(object sender, SqlRowUpdatedEventArgs e)
        {
            //throw new NotImplementedException();
        }

        private void Adapter_RowUpdating(object sender, SqlRowUpdatingEventArgs e)
        {
            //throw new NotImplementedException();
            if (e.Row.RowState != DataRowState.Deleted)
            {
                if (e.Row.RowState == DataRowState.Modified)
                {
                    if (!TBL_quyenBL.isThaydoi(EnumComponent.iEquipHistory))
                        e.Status = UpdateStatus.SkipCurrentRow;
                }
                dsLichSu.TBL_lichsuDataTable dt = dsLichSu.TBL_lichsu;
                if (e.Row[dt.ngayColumn] == DBNull.Value)
                {
                    MyBox.ShowWarning("Vui lòng chọn ngày!", "Quản lý lịch sử thiết bị");
                    e.Status = UpdateStatus.SkipAllRemainingRows;
                    return;
                }
            }
            if (e.Status == UpdateStatus.ErrorsOccurred)
            {
            }
        }

        private void gbList_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
                Deleted();
            }

            ProcessDialogKey(e.KeyCode);
        }

        private void Deleted()
        {
            if (!TBL_quyenBL.isThaydoi(EnumComponent.iEquipHistory)) return;

            if (MyBox.ShowQuestion("Bạn có chắc chắn muốn xóa không?", "Quản lý thiết bị") == DialogResult.No) return;

            if (gbList.SelectedRowsCount > 0)
            {
                gbList.DeleteSelectedRows();
            }
            else
            {
                gbList.DeleteRow(gbList.FocusedRowHandle);
            }

            gbList_RowUpdated(null, null);
        }

        private void gbList_RowUpdated(object sender, RowObjectEventArgs e)
        {
            Save();
            Nap();
            NotSave = false;
        }

        private void gbList_CellValueChanged(object sender, CellValueChangedEventArgs e)
        {
            gbList.ClearColumnErrors();

            // gbList.UpdateCurrentRow();
            if (m_Column == Column.Lock) return;
            if (m_Column == Column.None)
            {
                if (e.Column == colngay | e.Column == colnoidung)
                {
                    m_Column = Column.MaTB;
                }
            }

            switch (m_Column)
            {
                case Column.None:
                    return;
                case Column.MaTB:
                    m_Column = Column.Lock;
                    gbList.SetRowCellValue(e.RowHandle, colidthietbi, _thietbi.ID);
                    //gbList.SetRowCellValue(e.RowHandle, "idloaiLK",(short)Style);
                    //gbList.UpdateCurrentRow();
                    m_Column = Column.None;
                    return;
            }
        }

        protected override bool ProcessDialogKey(Keys keyData)
        {
            switch (keyData)
            {
                    //case Keys.Alt | Keys.D:
                    //    //MessageBox.Show("You are stupid");
                    //    return true;

                    //// some more cases...  

                    //case Keys.F1:
                    //    //ucToolBar_HelpClick(ucBar);
                    //    F11Execute();
                    //    return true;
                    //case Keys.F3:
                    //    F3Execute();
                    //    return true;
                    //case Keys.F4:
                    //    F4Execute();
                    //    return true;
                    //case Keys.F5:
                    //    F5Execute();
                    //    return true;
                    //case Keys.F6:
                    //    F9Execute();
                    //    return true;
                    //case Keys.F7:
                    //    F7Execute();
                    //    return true;
                    //case Keys.F8:
                    //    F8Execute();
                    //    return true;
                    //case Keys.F9:
                    //    F9Execute();
                    //    return true;
                    //case Keys.F11:
                    //    F11Execute();
                    //    return true;
                    //case Keys.F12:
                    //    F12Execute();
                    //    return true;

                case Keys.Escape:
                    ParentForm.Close();
                    EscapeExecute();
                    return true;

                case Keys.Alt | Keys.X:
                    ParentForm.Close();
                    return true;

                case Keys.F10:
                    F10Execute();
                    ParentForm.Close();
                    return true;
            }

            return false;
        }

        private void F10Execute()
        {
        }

        private void EscapeExecute()
        {
        }

        private void gbList_CustomDrawRowIndicator(object sender, RowIndicatorCustomDrawEventArgs e)
        {
            int rowIndex = e.RowHandle;
            if (rowIndex >= 0)
            {
                rowIndex++;
                e.Info.DisplayText = rowIndex.ToString();
            }
        }

        private void gbList_CellValueChanging(object sender, CellValueChangedEventArgs e)
        {
            NotSave = true;
        }
    }
}